﻿@using System.Data
@using STX.Domain
@model STX.Admin.Models.BizActivity.EditModel
@{
    ViewBag.Title = "Edit";
    Layout = null;
    STX.Domain.TabAdministrator CurUser = ViewBag.CurUser;
    int i = -1;
}
<style>
#editactivityform .tempQPT{ position: absolute; width: 200px; z-index: 9; display: none;}
#editactivityform .tempQPT ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
#editactivityform .tempQPT ul li{ border-bottom: #eee 1px solid;padding: 5px; cursor: pointer }
#editactivityform .tempQPT ul li:hover{ background:#f0e68c}
#editactivityform .tempQPT ul li.active{ background:#f0e68c}

#editactivityform .tempQuotePrice{ position: absolute; width: 200px; z-index: 9; display: none;}
#editactivityform .tempQuotePrice ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
#editactivityform .tempQuotePrice ul li{ border-bottom: #eee 1px solid;padding: 5px; }
</style>
<form class="pageForm addpage" action="@Url.Action("saveedit")" method="post" id="editactivityform">
    <input type="hidden" value="@Model.Id" name="Id" />
    <input type="hidden" value="@Model.MainAreaId" name="MainAreaId" />
    <input type="hidden" value="@Model.ProjectId" name="ProjectId" />
    <div style=" height:100%; width:100%; overflow:hidden" class="verticalSplitContainer">
        <div class="pageFormContent" ratio="*">
            <div class="inputActivityDiv">
                <table style="width:100%">
                    <tr>    
                        <td width="15%">所属项目：</td>
                        <td width="35%">@Model.ProjectName
                        </td>
                        <td width="15%">活动名称：</td>
                        <td width="35%"><input name="Name" class="textInput" type="text" size="40" value="@Model.Name"/></td>
                    </tr>
                    <tr>    
                        <td width="15%">活动时间：</td>
                        <td width="35%"> <input name="StartTime" class="textInput datetime" type="text" size="12" value="@Model.StartTime.ToString("yyyy-M-d")" alt=""/> - <input name="EndTime" class="textInput datetime" type="text" size="13" value="@Model.EndTime.ToString("yyyy-M-d")" alt=""/></td>
                        <td width="15%">活动地点：</td>
                        <td width="35%"><input name="Place" class="textInput" type="text" size="30"  alt="" value="@Model.Place"/></td>
                    </tr>
                    @*<tr>    
                            <td width="15%">报价制作人：</td>
                            <td width="35%"><input name="QuotationTabulator" class="textInput" type="text" size="30"  alt=""/></td>
                            <td width="15%">制表时间：</td>
                            <td width="35%"><input name="QuotationGeneratedTime" class="textInput datetime" type="text" size="15" value="" alt=""/></td>
                        </tr>
                        <tr>    
                            <td width="15%">成本制表人：</td>
                            <td width="35%"><input name="CostTabulator" class="textInput" type="text" size="30"  alt=""/></td>
                            <td width="15%">制表时间：</td>
                            <td width="35%"><input name="CostGeneratedTime" class="textInput datetime" type="text" size="15" value="" alt=""/></td>
                        </tr>*@
                    <tr>    
                        <td width="15%">负责人：</td>
                        <td width="35%"><input name="ResponsiblePerson" class="textInput" type="text" size="30"  alt="" value="@Model.ResponsiblePerson"/></td>
                        <td width="15%">执行：</td>
                        <td width="35%"><input name="Executor" class="textInput" type="text" size="30"  alt="" value="@Model.Executor"/></td>
                    </tr>
                    <tr>    
                        <td width="15%">策划：</td>
                        <td width="35%"><input name="Planner" class="textInput" type="text" size="30" alt="" value="@Model.Planner"/></td>
                        <td width="15%">司机：</td>
                        <td width="35%"><input name="Driver" class="textInput" type="text" size="30" alt="" value="@Model.Driver"/></td>
                    </tr>
                </table>
                <table style="width: 100%" id="tablepricelsit">
                        
                    <tr style="opacity:0">
                        <td style="width:100px;"><input name="" style="width:100px;" class="textInput" /></td>
                        <td style="width:200px;"><input name="" class="textInput" type="text" style="width:200px;"/> </td>
                        <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                        <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                        <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                        @if (CurUser.Position == PositionEnum.ProjectManager
                             || CurUser.Position == PositionEnum.SystemPower
                             || CurUser.Position == PositionEnum.GeneralManager)
                        {
                            <td style="width: 50px;"><input name="" class="textInput" type="text" style="width: 50px;"/></td>
                        }
                        @if (CurUser.Position == PositionEnum.FinancialManager
                             || CurUser.Position == PositionEnum.SystemPower
                             || CurUser.Position == PositionEnum.GeneralManager)
                        {
                            <td style="width: 50px;"><input name="" class="textInput" type="text" style="width: 50px;"/></td>
                        }
                        <td style="width:100px;"><input name="" style="width:100px;" class="textInput" /></td>
                        <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>(0, "select"))</td>
                        <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>(0, "select"))</td>
                        <td style="width:80px;"><input type="checkbox" />――――</td>
                    </tr>
                    <tr>
                        <td style="width:100px;"><h6 style="width:90px;">项目</h6></td>
                        <td style="width:200px;"><h6 style="width:190px;">具体要求</h6></td>
                        <td style="width:50px;"><h6 style="width:50px;">单位</h6></td>
                        <td style="width:50px;"><h6 style="width:50px;">数量</h6></td>
                        <td style="width:50px;"><h6 style="width:50px;">报价</h6></td>
                        @if (CurUser.Position == PositionEnum.ProjectManager
                             || CurUser.Position == PositionEnum.SystemPower
                             || CurUser.Position == PositionEnum.GeneralManager)
                        {
                            <td style="width: 50px;"><h6 style="width: 50px;">项目成本</h6></td>
                        }
                        @if (CurUser.Position == PositionEnum.FinancialManager
                             || CurUser.Position == PositionEnum.SystemPower
                             || CurUser.Position == PositionEnum.GeneralManager)
                        {
                            <td style="width: 50px;"><h6 style="width: 50px;">财务成本</h6></td>
                        }
                        <td style="width:100px;"><h6 style="width:50px;">供应商</h6></td>
                        <td style="width:50px;"><h6 style="width:50px;">物料形式</h6></td>
                        <td style="width:50px;"><h6 style="width:50px;">物料归属</h6></td>
                        <td><h6 style="width:50px;">状态</h6></td>
                    </tr>
                </table>
                @for (var j = 0; j < Model.MaterialCategories.Count; j++)
                {
                    var item = Model.MaterialCategories[j];
                    var activityItems = Model.BizActivityItems.Where(o => o.MaterialCategoryId == item.Id);

                    <div class="materialitem">
                        <h4 style=" background: #c0c0c0; padding: 6px;">@item.Name</h4>
                        <table style="width: 100%" id="activitytb(@item.Id)">
                                
                            @{
                                   var bizActivityItems = activityItems as BizActivityItem[] ?? activityItems.ToArray();
                            }
                            @if (!bizActivityItems.Any())
                            {
                                i++;
                                <tr>
                                    <td style="width: 100px;" style="position: relative"><input onkeyup=" getTemplateData(event, this) " name="BizActivityItems[@i].Name" style="width: 100px;" class="textInput" type="text"/></td>
                                    <td style="width: 200px;"><input name="BizActivityItems[@i].Require" class="textInput" type="text" style="width: 200px;"/> </td>
                                    <td style="width: 50px;"><input name="BizActivityItems[@i].Unit" class="textInput" type="text" style="width: 50px;" /></td>
                                    <td style="width: 50px;" style="position: relative"><input name="BizActivityItems[@i].Numb" class="textInput" type="text" style="width: 50px;" value="0" onkeyup=" calculate(this) " onfocus=" showQuotePrice(this) "  onblur=" hideQuotePrice(this) "/></td>
                                    <td style="width: 50px;"><input name="BizActivityItems[@i].QuotationPrice" class="textInput" type="text" style="width: 50px;" value="0" alt=""/></td>
                                    @if (CurUser.Position == PositionEnum.ProjectManager
                                         || CurUser.Position == PositionEnum.SystemPower
                                         || CurUser.Position == PositionEnum.GeneralManager)
                                    {
                                        <td style="width: 50px;"><input name="BizActivityItems[@i].ProjectCostPrice" class="textInput" type="text" style="width: 50px;" value="0" alt=""/></td>
                                    }
                                    @if (CurUser.Position == PositionEnum.FinancialManager
                                         || CurUser.Position == PositionEnum.SystemPower
                                         || CurUser.Position == PositionEnum.GeneralManager)
                                    {
                                        <td style="width: 50px;"><input name="BizActivityItems[@i].FinanceCostPrice" class="textInput" type="text" style="width: 50px;" value="0" alt=""/></td>
                                    }
                                    <td style="width: 100px;"><input name="BizActivityItems[@i].SupplierName" style="width: 100px;" class="textInput" type="text"/></td>
                                    <td style="width: 50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>(0, "BizActivityItems[" + @i + "].MaterialForm"))</td>
                                    <td style="width: 50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>(0, "BizActivityItems[" + @i + "].MaterialBelong")) <input type="hidden" name="BizActivityItems[@i].QuotePriceId" value="0"/><input type="hidden" name="BizActivityItems[@i].MaterialCategoryId" value="@item.Id"/></td>
                                    <td style="width: 80px;"><input type="checkbox" name="BizActivityItems[@i].ErrorStatus" value="0" disabled="disabled"/></td>
                                </tr>
                            }
                            else
                            {
                                foreach (var activityItem in bizActivityItems)
                                {
                                    i++;
                                    var curColor = "";
                                    var curReadonly="";
                                    if (activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Error)
                                    {
                                        curColor = " style=background:#ff3300";
                                    }
                                    if (activityItem.QuotePriceId > 0)
                                    {
                                        curReadonly = " readonly=readonly";
                                    }
                                    <tr@curColor>
                                        <td style="width: 100px;" style="position: relative">
                                            <input onkeyup=" getTemplateData(event, this) " name="BizActivityItems[@i].Name" style="width: 100px;" class="textInput" type="text" value="@activityItem.Name"/>
                                        </td>
                                        <td style="width: 200px;">
                                            <input name="BizActivityItems[@i].Require" class="textInput" type="text" style="width: 200px;" value="@activityItem.Require"@curReadonly/> 
                                        </td>
                                        <td style="width: 50px;">
                                            <input name="BizActivityItems[@i].Unit" class="textInput" type="text" style="width: 50px;" value="@activityItem.Unit"@curReadonly/>
                                        </td>
                                        <td style="width: 50px;" style="position: relative">
                                            <input name="BizActivityItems[@i].Numb" class="textInput" type="text" style="width: 50px;" value="@activityItem.Numb" onkeyup=" calculate(this) " onfocus=" showQuotePrice(this) "  onblur=" hideQuotePrice(this) "@curReadonly/>
                                        </td>
                                        <td style="width: 50px;"><input name="BizActivityItems[@i].QuotationPrice" class="textInput" type="text" style="width: 50px;" value="@activityItem.QuotationPrice" alt=""@curReadonly/></td>
                                        @if (CurUser.Position == PositionEnum.ProjectManager
                                             || CurUser.Position == PositionEnum.SystemPower
                                             || CurUser.Position == PositionEnum.GeneralManager)
                                        {
                                            <td style="width: 50px;"><input name="BizActivityItems[@i].ProjectCostPrice" class="textInput" type="text" style="width: 50px;" value="@activityItem.ProjectCostPrice" alt=""@curReadonly/></td>
                                        }
                                        @if (CurUser.Position == PositionEnum.FinancialManager
                                             || CurUser.Position == PositionEnum.SystemPower
                                             || CurUser.Position == PositionEnum.GeneralManager)
                                        {
                                            <td style="width: 50px;"><input name="BizActivityItems[@i].FinanceCostPrice" class="textInput" type="text" style="width: 50px;" value="@activityItem.FinanceCostPrice" alt=""@curReadonly/></td>
                                        }
                                        <td style="width: 100px;"><input name="BizActivityItems[@i].SupplierName" style="width: 100px;" class="textInput" type="text" value="@activityItem.BizSupplier.Name"/></td>
                                        <td style="width: 50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>((int)activityItem.MaterialForm, "BizActivityItems[" + @i + "].MaterialForm"))</td>
                                        <td style="width: 50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>((int)activityItem.MaterialBelong, "BizActivityItems[" + @i + "].MaterialBelong")) 
                                            <input type="hidden" name="BizActivityItems[@i].QuotePriceId" value="@activityItem.QuotePriceId"/>
                                            <input type="hidden" name="BizActivityItems[@i].SupplierId" value="@activityItem.SupplierId"/>
                                            <input type="hidden" name="BizActivityItems[@i].MaterialCategoryId" value="@item.Id"/>
                                            
                                            </td>
                                        <td style="width: 80px;">
                                        @if ((CurUser.Position == PositionEnum.FinancialManager
                                             || CurUser.Position == PositionEnum.ProjectManager
                                             || CurUser.Position == PositionEnum.SystemPower
                                             || CurUser.Position == PositionEnum.GeneralManager
                                             ) && (activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Correct
                                             || activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.None))
                                        {
                                            <span><input type="checkbox" name="BizActivityItems[@i].ErrorStatus" value="1" />有问题。</span>
                                        }
                                        @if ((CurUser.Position == PositionEnum.FinancialManager
                                             || CurUser.Position == PositionEnum.ProjectManager
                                             || CurUser.Position == PositionEnum.SystemPower
                                             || CurUser.Position == PositionEnum.Salesperson
                                             ) && (activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Error))
                                        {
                                            <span><input type="checkbox" name="BizActivityItems[@i].ErrorStatus " value="2" />已改正。</span>
                                        }
                                        </td>
                                    </tr>
                                    
                                }
                                
                                
                            }

                        </table>
                        <div style="padding: 0 0 10px 0"><a href="javascript:;" title="添加一行" class="addtr" style="font-size: 16px; font-weight: bold; text-decoration: none; color: #228b22">+</a>&nbsp;&nbsp;<a href="javascript:;" title="删去一行" class="removetr" style="font-size: 16px; font-weight: bold; text-decoration: none; color:crimson">-</a></div>
                    </div>
                            
                }
            </div>
        </div>

        <div class="formBar" ratio="#30">
            <ul>
                
                @if (CurUser.Position == PositionEnum.Salesperson)
                {
                    if (Model.Step == BizActivity.StepEnum.销售员报价阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.AuditingEnum.待审核)" name="Auditing" />
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">保存</button></div></div></li>
                    }
                    if (Model.Step == BizActivity.StepEnum.销售员报价阶段 && Model.Auditing == BizActivity.AuditingEnum.审核失败)
                    {
                        <input type="hidden" value="@((int)BizActivity.AuditingEnum.待审核)" name="Auditing" />
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">重新提交</button></div></div></li>    
                    }
                    if (Model.Step == BizActivity.StepEnum.销售员报价阶段 && Model.Auditing == BizActivity.AuditingEnum.审核成功)
                    {
                        <input type="hidden" value="@((int)BizActivity.AuditingEnum.待审核)" name="Auditing" />
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员成本阶段)" name="Step" />
                        <li><div class="simplebutton"><div class="r"><button type="button">打印报价表</button></div></div></li>
                        <li><div class="simplebutton"><div class="r"><button type="button">导出报价表</button></div></div></li>
                        <li><div class="simplebutton"><div class="r"><button type="button">提交成本表</button></div></div></li>
                    }
                    if (Model.Step == BizActivity.StepEnum.销售员成本阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.AuditingEnum.待审核)" name="Auditing" />
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员成本阶段)" name="Step" />
                        <li><div class="simplebutton"><div class="r"><button type="button">打印报价表</button></div></div></li>
                        <li><div class="simplebutton"><div class="r"><button type="button">导出报价表</button></div></div></li>
                        <li><div class="simplebutton"><div class="r"><button type="button">保存</button></div></div></li>
                    }
                }
                @if (CurUser.Position == PositionEnum.ProjectManager)
                {
                    if (Model.Step == BizActivity.StepEnum.销售员报价阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li>@(Html.CheckBoxFromEnum<BizActivity.AuditingEnum>(-1, "Step"))</li>
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                    }
                    if (Model.Step == BizActivity.StepEnum.销售员成本阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li>@(Html.CheckBoxFromEnum<BizActivity.AuditingEnum>(-1, "Step"))</li>
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                    }
                }
                @if (CurUser.Position == PositionEnum.FinancialManager)
                {
                    if (Model.Step == BizActivity.StepEnum.项目经理阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li>@(Html.CheckBoxFromEnum<BizActivity.AuditingEnum>(-1, "Step"))</li>
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                    }
                }
                @if (CurUser.Position == PositionEnum.GeneralManager)
                {
                    if (Model.Step == BizActivity.StepEnum.财务阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                        <li>@(Html.CheckBoxFromEnum<BizActivity.AuditingEnum>(-1, "Step"))</li>
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                    }
                }
                 @if (CurUser.Position == PositionEnum.SystemPower)
                {
                    if (Model.Step == BizActivity.StepEnum.销售员报价阶段 && Model.Auditing == BizActivity.AuditingEnum.待审核)
                    {
                        <input type="hidden" value="@((int)BizActivity.StepEnum.销售员报价阶段)" name="Step" />
                       
                        <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                        <li>@(Html.DropDownListFromEnum<BizActivity.AuditingEnum>(-1, "Auditing"))</li>
                    }
                }
                <li>【目前状态：@Model.Step.GetFiledName()（@Model.Auditing.GetFiledName()）】</li>
            </ul>
        </div>
    </div>
    <script type="text/javascript">
        function keyUp(obj) {
            var $ul = $(obj).parent().find("ul");
            var $active = $ul.find(".active");
            if ($ul.find(".active").size() == 0) {
                $ul.find("li").first().addClass("active");
                return;
            }
            var index = $active.index();
            index--;
            if (index == -1) {
                index = $ul.find("li").size() - 1;
            }
            $ul.find("li").removeClass("active");
            $ul.find("li").eq(index).addClass("active");
        }

        function keyDown(obj) {
            var $ul = $(obj).parent().find("ul");
            var $active = $ul.find(".active");
            if ($ul.find(".active").size() == 0) {
                $ul.find("li").first().addClass("active");
                return;
            }
            var index = $active.index();
            index++;
            if (index == $ul.find("li").size()) {
                index = 0;
            }
            $ul.find("li").removeClass("active");
            $ul.find("li").eq(index).addClass("active");
        }

        function keyEnter(obj) {
            var $ul = $(obj).parent().find("ul");
            if ($ul.find(".active").size() == 0) {
                return;
            }
            SetTrData(obj);
        }

        function SetTrData(obj) {
            var $ul = $(obj).parent().find("ul");
            var $active = $ul.find(".active");
            if ($ul.find(".active").size() == 0) {
                $(obj).parent().removeAttr("data");
                return;
            }
            $(obj).parent().parent().attr("data", $active.attr("data"));
            $ul.parent().remove();
            SetInput($(obj).parent());
        }

        function SetInput($td) {
            var $tr = $td.parent();
            var data = $tr.attr("data");
            if (!data) {
                $tr.find("input[name*='Require']").removeAttr("readonly").val('');
                $tr.find("input[name*='Unit']").removeAttr("readonly").val('');
                $tr.find("input[name*='QuotationPrice']").removeAttr("readonly").val('');
                $tr.find("input[name*='QuotePriceId']").val('0');
                $tr.find("input[name*='SupplierName']").removeAttr("readonly").val('');
                return;
            }
            var json = JSON.parse(data);
            
            $tr.find("input[name*='Name']").val(json.Name);
            $tr.find("input[name*='Require']").attr("readonly", "readonly").val(json.Required);
            $tr.find("input[name*='Unit']").attr("readonly", "readonly").val(json.Unit);
            $tr.find("input[name*='SupplierName']").attr("readonly", "readonly").val(json.Supplier.Name);
            showQuotePrice($tr.find("input[name*='Numb']").get());
            $tr.find("input[name*='Numb']").select();
        }


        function showQuotePrice(obj) {
            var $this = $(obj);
            var $td = $this.parent();
            var $tr = $this.parent().parent();
            var data = $tr.attr("data");
            if (!data) {
                return;
            }
            var json = JSON.parse(data);
            var $quotePrice = $td.find(".tempQuotePrice");
            if ($quotePrice.size() == 0) {
                $("<div class='tempQuotePrice'><ul></ul></div>").appendTo($td);
            }
            var $box = $td.find(".tempQuotePrice");
            $box.show().find("ul").empty();
            $.each(json.QuotePrices, function(i, d) {
                var $li = $('<li>数目：' + d.StartNum + ' 至 ' + d.EndNum + ' 价格：' + d.QuotePrice + '</li>');
                $box.find("ul").append($li);
            });
        }

        function hideQuotePrice(obj) {
            var $this = $(obj);
            var $td = $this.parent();
            $td.find(".tempQuotePrice").hide();
            calculate(obj);
        }

        function calculate(obj) {
            var $this = $(obj);
            var val = $this.val();
            if (!val.isNumber) {
                alert('需输入纯数字');
                return;
            }
            
            var $td = $this.parent();
            var $tr = $td.parent();
            var data = $tr.attr("data");
            if (!data)return;
            var json = JSON.parse(data);
            $.each(json.QuotePrices, function(i, d) {
                if (val <= d.EndNum && val >= d.StartNum) {
                    $tr.find("input[name*='QuotePriceId']").val(d.Id);
                    $tr.find("input[name*='QuotationPrice']").attr("readonly", "readonly").val(d.QuotePrice);
                    if (d.ProjectCostPrice) {
                        $tr.find("input[name*='ProjectCostPrice']").attr("readonly", "readonly").val(d.ProjectCostPrice);
                    }
                    if (d.FinanceCostPrice) {
                        $tr.find("input[name*='FinanceCostPrice']").attr("readonly", "readonly").val(d.FinanceCostPrice);
                    }
                }
            });     
           
        }

        function getTemplateData(e, obj) {

            if (e.keyCode == 38) {
                keyUp(obj);
                return;
            }
            if (e.keyCode == 40) {
                keyDown(obj);
                return;
            }
            if (e.keyCode == 13) {
                keyEnter(obj);
                return;
            }

            var $this = $(obj);
            var $td = $this.parent();
            var val = $this.val();
            if (!val) {
                $td.parent().removeAttr("data");
                $td.find(".tempQPT").remove();
                SetInput($td);
                return;
            }

            if (val == $this.attr("oldvalue")) {
                return;
            }
            $this.attr("oldvalue", val);
            var data = { key: val };
            var option = {
                url: "@Url.Action("GetQuotePriceTemplateData")",
                data: data,
                type: "POST",
                dataType: "json",
                success: function(json) {
                    if (!json.success) {
                        $td.parent().removeAttr("data");
                        $td.find(".tempQPT").remove();
                        SetInput($td);
                        return;
                    } else {
                        var $qpt = $td.find(".tempQPT");
                        if ($qpt.size() == 0) {
                            $("<div class='tempQPT'><ul></ul></div>").appendTo($td);
                        }
                        var $box = $td.find(".tempQPT");
                        $box.show().find("ul").empty();
                        $.each(json.list, function(i, d) {
                            var $li = $('<li>' + d.Name + ' (供应商：' + d.Supplier.Name + ')</li>');
                            $li.attr("data", JSON.stringify(d));
                            $box.find("ul").append($li);
                        });

                        //绑定li click 事件
                        $box.find("li").click(function() {
                            $box.find("li").removeClass("active");
                            $(this).addClass("active");
                            SetTrData(obj);
                        });
                    }
                }
            };
            $.ajax(option);


        }


        $(document).bind("click", function(e) {
            var target = $(e.target);
            if (target.closest(".tempQPT").length == 0) {
                $(".tempQPT").hide();
            }
        });

        $(function() {

            //将标错行背景置红
            $("input[name*='ErrorStatus']", "#editactivityform .materialitem").click(function() {
                var val = $(this).val();
                //alert(val);
            });
                
            function AddOrRemoveCallback($thisarea) {
                $("table tr", "#editactivityform .materialitem").each(function (i) {
                    $(this).find("input,select").each(function () {
                        var $thisinput = $(this);
                        $thisinput.attr("name", $thisinput.attr("name").replace(/\d+/g, i));
                    });
                });
            }

            $(".addtr").click(function (e) {
                var $thisMaterialItem = $(this).parentsUntil("#editactivityform .materialitem").parent();
                var $tr = $("table tr:last", $thisMaterialItem).clone();
                $tr.appendTo($("table", $thisMaterialItem));
                AddOrRemoveCallback($thisMaterialItem);
               
                $thisMaterialItem.find("tr:last").removeAttr("style").find("input:first").select();
                $thisMaterialItem.find("tr:last").find("td:last").empty();
            });

            $(".removetr").click(function () {
                var $thisMaterialItem = $(this).parentsUntil("#editactivityform .materialitem").parent();
                if ($("tr", $thisMaterialItem).size() > 1) {
                    $("tr:last", $thisMaterialItem).remove();
                    AddOrRemoveCallback($thisMaterialItem);
                }
            });
        });
    </script>
</form>

